package com.ai.module.mj.config;

import cn.hutool.core.io.IoUtil;
import com.ai.module.mj.config.propertites.YmlProperties;
import com.ai.module.mj.domain.Task;
import com.ai.module.mj.helper.DiscordAccountHelper;
import com.ai.module.mj.loadbalancer.rule.IRule;
import com.ai.module.mj.loadbalancer.rule.impl.BestWaitIdleRule;
import com.ai.module.mj.loadbalancer.rule.impl.RoundRobinRule;
import com.ai.module.mj.service.HighTaskStoreService;
import com.ai.module.mj.service.NotifyService;
import com.ai.module.mj.service.TranslateService;
import com.ai.module.mj.service.impl.translate.BaiduTranslateServiceImpl;
import com.ai.module.mj.service.impl.translate.NoTranslateServiceImpl;
import com.ai.module.mj.util.DiscordURLHelper;
import com.ai.module.mj.wss.handle.MessageHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ai.module.mj.constant.stringConstant.StringConstant.WAIT_RULE;

/**
 * @author: 何晋庆
 * @create: 2023-12-10 12:55
 * @description: 根据不同的配置 差异化实例化
 **/
@Configuration
public class BeanConfig {
	@Autowired
	private ApplicationContext applicationContext;
	@Autowired
	private YmlProperties properties;

	@Bean
	TranslateService translateService() {
		 switch (this.properties.getTranslateWay()) {
			 case BAIDU:
				 return new BaiduTranslateServiceImpl(this.properties.getBaiduTranslate());
			 default:
				 return new NoTranslateServiceImpl();
		}
	}

	//@Bean
//	TaskStoreService taskStoreService(RedisConnectionFactory redisConnectionFactory) {
//		YmlProperties.TaskStore.Type type = this.properties.getTaskStore().getType();
//		Duration timeout = this.properties.getTaskStore().getTimeout();
//		 switch (type) {
//			 case IN_MEMORY:
//				 return new InMemoryTaskStoreServiceImpl(timeout);
//			 //case REDIS:
//			 default:
//				 return new RedisHighTaskStoreServiceImpl(timeout, taskRedisTemplate(redisConnectionFactory));
//		}
//	}

	@Bean
	RedisTemplate<String, Task> taskRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
		RedisTemplate<String, Task> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(redisConnectionFactory);
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Task.class));
		return redisTemplate;
	}

	@Bean
	public IRule loadBalancerRule() {
		switch (this.properties.getAccountChooseRule()){
			case WAIT_RULE:
				return new BestWaitIdleRule();
			default:
				return new RoundRobinRule();
		}

	}

	@Bean
	List<MessageHandler> messageHandlers() {
		return new ArrayList<>(this.applicationContext.getBeansOfType(MessageHandler.class).values());
	}

	@javax.annotation.Resource(name = "restTemplate")
	private RestTemplate restTemplate;

	@Bean
	DiscordAccountHelper discordAccountHelper(DiscordURLHelper discordURLHelper, HighTaskStoreService taskStoreService, NotifyService notifyService) throws IOException {
		Resource[] resources = this.applicationContext.getResources("classpath:api-params/*.json");
		Map<String, String> paramsMap = new HashMap<>();
		for (Resource resource : resources) {
			String filename = resource.getFilename();
			String params = IoUtil.readUtf8(resource.getInputStream());
			paramsMap.put(filename.substring(0, filename.length() - 5), params);
		}
		return new DiscordAccountHelper(discordURLHelper, this.properties, restTemplate , taskStoreService, notifyService, messageHandlers(), paramsMap);
	}


}
